<template>
    <Header :user="user"/>
    <div id="page" class="hfeed site ">
        <header id="masthead" class="site-header" role="banner">
            <div id="karma-header" class="">
                <div class="header-inner">
                    <div class="container">
                        <div class="row">
                            <div class="karma-branding">
                                <div id="karma-logo" class="show">
                                    <a href="/" class="custom-logo-link" rel="home" itemprop="url">
                                        <img src="https://assets.yanghaha.vip/static/images/blog_logo.png"
                                             class="custom-logo" alt="YangHaHa博客" itemprop="logo"/>
                                    </a>
                                </div>
                                <h1 class="site-title hidden">
                                    <a href="javascript:void(0);" rel="home">彩蛋</a>
                                </h1>
                                <p class="site-description hidden">彩蛋~</p>
                            </div>
                            <div class="karma-header-menu">
                                <nav id="site-navigation" class="main-navigation" role="navigation">
                                    <div class="menu-top-menu-container">
                                        <ul id="primary-menu" class="karma-nav">
                                            <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children">
                                                <a href="javascript:void(0)">技术</a>
                                                <ul class="sub-menu">
                                                    <li class="menu-item menu-item-type-post_type menu-item-object-service">
                                                        <router-link :to="{path:'/list/1'}">前端</router-link>
                                                    </li>
                                                    <li class="menu-item menu-item-type-post_type menu-item-object-service">
                                                        <router-link :to="{path:'/list/2'}">后端</router-link>
                                                    </li>
                                                    <li class="menu-item menu-item-type-post_type menu-item-object-service">
                                                        <router-link :to="{path:'/list/3'}">数据库</router-link>
                                                    </li>
                                                </ul>
                                            </li>
                                            <li class="menu-item menu-item-type-post_type menu-item-object-service">
                                                <router-link :to="{path:'/list/4'}">电影</router-link>
                                            </li>

                                            <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children"
                                                v-show="JSON.stringify(user) !== '{}'">
                                                <a href="javascript:void(0);">{{ user.username }}(积分:{{ user.point
                                                    }})</a>
                                                <ul class="sub-menu">
                                                    <li class="menu-item menu-item-type-post_type menu-item-object-service">
                                                        <a href="javascript:void(0);" @click.prevent="logout">登出</a>
                                                    </li>
                                                </ul>
                                            </li>
                                            <li v-show="JSON.stringify(user) === '{}'"
                                                class="menu-item menu-item-type-post_type menu-item-object-service">
                                                <a href="#login">登录</a>
                                            </li>
                                        </ul>
                                    </div>
                                </nav>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </header>
        <div id="content" class="site-content">
            <div id="karma-page-jumbotron" class="table-display">
                <div id="karma-jumbo-js"></div>
                <div class="cell-display">
                    <div class="container">
                        <div class="row">
                            <div class="col-sm-12">
                                <header class="entry-header centered">
                                    <h1 v-if="common?.setting?.short_word" class="entry-title">
                                        {{ common.setting.short_word }}</h1>
                                    <h1 v-else class="entry-title">欢迎来访</h1>
                                </header>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div id="primary" class="content-area">
                <router-view/>
            </div>
        </div>
        <Footer :common="common"/>
    </div>
</template>

<script>
    import BaseService from "@/services/BaseService";
    import Header from "../components/Header";
    import Footer from "../components/Footer";

    const BaseServiceApi = new BaseService();
    export default {
        name: "Index",
        components: {Footer, Header},
        data: function () {
            return {
                user: {},
                common: {},
            };
        },
        created: function () {
            this.checkLogin();
            this.getCommon();
        },
        methods: {
            checkLogin: function () {
                let token = BaseServiceApi.getStorage("token");
                let header = {
                    Authorization: token
                };
                if (token) {
                    BaseServiceApi.getApi("/api/v1/check", {}, header).then(res => {
                        if (res.data.code === 200) {
                            this.user = res.data.data;
                        } else {
                            BaseServiceApi.removeStorage("token")
                        }
                    })
                }
            },
            getCommon: function () {
                BaseServiceApi.getApi("/api/v1/config").then(res => {
                    if (res.data.code === 200) {
                        this.common = res.data.data;
                        document.title = this.common.setting.title;
                        document.querySelector('meta[name="description"]').setAttribute('content',this.common.setting.description);
                        document.querySelector('meta[name="keywords"]').setAttribute('content',this.common.setting.keyword);
                    }
                });
            },
            logout: function () {
                BaseServiceApi.removeStorage("token");
                window.location.reload();
            }
        }
    }
</script>

<style scoped>

</style>
